﻿using Newtonsoft.Json;
using SouthKoreaCoffee.IOModel;
using SouthKoreaCoffee.Model;
using SouthKoreaCoffee.SocketClientsModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;

namespace SouthKoreaCoffee.RobotModel
{
    /// <summary>
    /// 机器人控制
    /// </summary>
    public class RobotOperate
    {
        public static string RobotOperateUrl = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
        public static int handle1 = 0;
        public static int handle2 = 0;
        public static double PI = 3.1415926535;
        public static List<string[]> listStrArr1 = new List<string[]>();//数组List，相当于可以无限扩大的二维数组。
        public static List<string[]> listStrArr2 = new List<string[]>();
        public static List<string[]> listStrArr3 = new List<string[]>();//数组List，相当于可以无限扩大的二维数组。
        public static List<string[]> listStrArr4 = new List<string[]>();
        public static List<string[]> listStrArr5 = new List<string[]>();//数组List，相当于可以无限扩大的二维数组。
        public static List<string[]> listStrArr6 = new List<string[]>();
        private static RobotOperate instance;
        private RobotOperate() { }
        public static RobotOperate Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new RobotOperate();
                }
                return instance;
            }
        }
        //public static JKTYPE.ProgramState pstatusR;
        //public static JKTYPE.ProgramState pstatusL;
        //public static JKTYPE.RobotState stateL;
        //public static JKTYPE.RobotStatus statusL;
        //public static JKTYPE.RobotState stateR;
        //public static JKTYPE.RobotStatus statusR;
        //public static void CX_Start_L()
        //{
        //    try
        //    {
        //        jakaAPI.get_program_state(ref handle1, ref pstatusL);
        //    }
        //    catch (Exception)
        //    {
        //        return;
        //    }
        //}
        //public static void CX_kj_L()
        //{
        //    try
        //    {
        //        jakaAPI.get_robot_state(ref handle1, ref stateL);
        //    }
        //    catch (Exception)
        //    {
        //        return;
        //    }
        //}
        //public static void CX_status_L()
        //{
        //    try
        //    {
        //        jakaAPI.get_robot_status(ref handle1, ref statusL);
        //    }
        //    catch (Exception)
        //    {
        //        return;
        //    }
        //}
        //public static void CX_Start_R()
        //{
        //    try
        //    {
        //        jakaAPI.get_program_state(ref handle2, ref pstatusR);
        //    }
        //    catch (Exception)
        //    {
        //        return;
        //    }
        //}
        //public static void CX_kj_R()
        //{
        //    try
        //    {
        //        jakaAPI.get_robot_state(ref handle2, ref stateR);
        //    }
        //    catch (Exception)
        //    {
        //        return;
        //    }
        //}
        //public static void CX_status_R()
        //{
        //    try
        //    {
        //        jakaAPI.get_robot_status(ref handle2, ref statusR);
        //    }
        //    catch (Exception)
        //    {
        //        return;
        //    }
        //}
        //public static void CX_Stop()
        //{
        //    try
        //    {
        //        jakaAPI.program_abort(ref handle1);
        //    }
        //    catch (Exception)
        //    {
        //        return;
        //    }
        //}
        //public static void CX_Stop_R()
        //{
        //    try
        //    {
        //        jakaAPI.program_abort(ref handle2);
        //    }
        //    catch (Exception)
        //    {
        //        return;
        //    }
        //}
        //public static void Robot_Yun_R()
        //{
        //    try
        //    {
        //        jakaAPI.program_run(ref handle2);
        //    }
        //    catch (Exception)
        //    {
        //        return;
        //    }
        //}
        public static void Read_CSV()//读取轨迹文件
        {
            string line1 = "";
            string line2 = "";
            string line3 = "";
            string line4 = "";
            string line5 = "";
            string line6 = "";
            string path1 = RobotOperateUrl + "\\cup_huibo_swan.csv";
            StreamReader reader1 = new StreamReader(path1);

            line1 = reader1.ReadLine();
            while (line1 != null)
            {
                listStrArr1.Add(line1.Split(','));//将文件内容分割成数组
                line1 = reader1.ReadLine();
            }
            reader1.Close();

            StreamReader reader2 = new StreamReader(RobotOperateUrl + "/milk_huibo_swan.csv");
            line2 = reader2.ReadLine();
            while (line2 != null)
            {
                listStrArr2.Add(line2.Split(','));//将文件内容分割成数组
                line2 = reader2.ReadLine();
            }
            reader2.Close();
            StreamReader reader5 = new StreamReader(RobotOperateUrl + "/cup_huibo_tulip.csv");
            line5 = reader5.ReadLine();
            while (line5 != null)
            {
                listStrArr5.Add(line5.Split(','));//将文件内容分割成数组
                line5 = reader5.ReadLine();
            }
            reader5.Close();
            StreamReader reader6 = new StreamReader(RobotOperateUrl + "/milk_huibo_tulip.csv");
            line6 = reader6.ReadLine();
            while (line6 != null)
            {
                listStrArr6.Add(line6.Split(','));//将文件内容分割成数组
                line6 = reader6.ReadLine();
            }
            reader6.Close();
        }
        public void Robot_Initialisation_double()//机器人初始化
        {
            IOMsg.Instance.AllBol = false;
            Console.WriteLine(DigitalMsg.Instance.RobotPowerOn_start);
            Thread.Sleep(200);
            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.RobotPowerOn_start);
            Thread.Sleep(3000);
            int result1 = -1;
            while (result1 == -1)
            {
                result1 = jakaAPI.create_handler(IPorPortInfo.Instance.RobotIPLeft.ToCharArray(), ref handle1);
                Thread.Sleep(100);
            }
            IOMsg.Instance.AllBol = false;
            Thread.Sleep(100);
            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.RobotPowerOn_end);
            Thread.Sleep(100);
            IOMsg.Instance.AllBol = true;
            Thread.Sleep(3000); //毫秒
            //机器人上电
            jakaAPI.power_on(ref handle1); Console.WriteLine("L_Robot Power On");
            Thread.Sleep(3000); //毫秒
            #region TCP方式
            File.WriteAllText(MainWindow.WarnPath + @"\" + DateTime.Now.ToString("yyyyddMMHHmmss") + ".txt", JsonConvert.SerializeObject("链接开始"));
            Thread thr1 = new Thread(IOMsg.Instance.IOSerial_Robot_Thr);
            thr1.IsBackground = true;
            thr1.Start();
            #endregion
            Console.WriteLine("SDK 版本 ");
            Thread.Sleep(2000); //毫秒
            while (!IOMsg.Instance.isRobotBol) { Thread.Sleep(100); }
            File.WriteAllText(MainWindow.WarnPath + @"\" + DateTime.Now.ToString("yyyyddMMHHmmss") + ".txt", JsonConvert.SerializeObject("链接结束"));
            ClientList clientList2 = new ClientList();
            clientList2.MsgBol = WuLiaoCoffeeSel.Instance;
            clientList2.code = 13;
            SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList2)));
            Thread thr2 = new Thread(MainWindow.MessageShow);
            thr2.IsBackground = true;
            thr2.Start();
        }
        /// <summary>
        /// 左臂状态查询
        /// </summary>
        public void Robot_L_Show()
        {
            bool RobotYX = RobotModelLeft.Instance.LRobotJQRYX_Sel();//运行
            bool RobotBJ = RobotModelLeft.Instance.LRobotJQRBJ_Sel();//报警
            bool RobotKJ = RobotModelLeft.Instance.LRobotJQRYKJ_Sel();//开机
            bool RobotSD = RobotModelLeft.Instance.LRobotJQRYSD_Sel();//上电
            bool RobotSN = RobotModelLeft.Instance.LRobotJQRYSN_Sel();//使能
            bool RobotZT = RobotModelLeft.Instance.LRobotJQRZT_Sel();//暂停
            bool RobotYD = RobotModelLeft.Instance.LRobotDYD_Sel();//运动
            if (RobotSD)
                RobotMsgs(false, true, false, false, false, false, false);
            if (RobotSN)
                RobotMsgs(false, false, true, false, false, false, false);
            if (RobotYX)
                RobotMsgs(false, false, false, true, false, false, false);
            if (RobotZT)
                RobotMsgs(false, false, false, false, false, true, false);
            if (!RobotSN)
                RobotMsgs(false, true, false, false, false, false, false);
            if (!RobotSD)
                RobotMsgs(true, false, false, false, false, false, false);
            if (RobotBJ)
                RobotMsgs(false, false, false, false, false, false, true);
        }
        /// <summary>
        /// 机器人状态
        /// </summary>
        /// <param name="KJ">开机</param>
        /// <param name="SD">上电</param>
        /// <param name="SN">使能</param>
        /// <param name="YX">运行</param>
        /// <param name="TZ">停止</param>
        /// <param name="ZT">暂停</param>
        /// <param name="BJ">报警</param>
        ///  <param name="type">左右手 0 左 1 右</param>
        public void RobotMsgs(bool KJ, bool SD, bool SN, bool YX, bool TZ, bool ZT, bool BJ, int type = 0)
        {
            if (type == 0)
            {
                RobotMsg.Instance.Robot_YKJ_L = KJ;
                RobotMsg.Instance.Robot_SD_L = SD;
                RobotMsg.Instance.Robot_SN_L = SN;
                RobotMsg.Instance.Robot_YX_L = YX;
                RobotMsg.Instance.Robot_TZ_L = TZ;
                RobotMsg.Instance.Robot_ZT_L = ZT;
                RobotMsg.Instance.Robot_BJ_L = BJ;
            }
            else
            {
                RobotMsg.Instance.Robot_YKJ_R = KJ;
                RobotMsg.Instance.Robot_SD_R = SD;
                RobotMsg.Instance.Robot_SN_R = SN;
                RobotMsg.Instance.Robot_YX_R = YX;
                RobotMsg.Instance.Robot_TZ_R = TZ;
                RobotMsg.Instance.Robot_ZT_R = ZT;
                RobotMsg.Instance.Robot_BJ_R = BJ;
            }
        }
    }
}
